/**
 * Project Name:javaBaseReview
 * Package Name:com.xuwei.base.thread
 * author:David
 * Date:2015年8月19日下午8:45:46
 * Copyright (c) 2015, xw123box@126.com All Rights Reserved.
 *
 */
package com.xuwei.base.thread;

/**
 * 传统线程同步通信技术
 * @author David
 * @since 2015年8月19日 下午8:45:46
 * @version 
 * @since JDK 1.6
 */
public class TraditionalThreadCommunication {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		final Business business=new Business();
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				for(int i=1;i<=10;i++){
					business.sub(i);
				}
				
			}
		}).start();
		
		for(int i=1;i<=10;i++){
			business.main(i);
		}
		
	}

}

class Business{
	private boolean bShouldSub=true;
	public synchronized void sub(int i){
		while(!bShouldSub){
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		for(int j=1;j<=10;j++){
			System.out.println("sub thread sequence of "+j+",loop of "+i);
		}
		bShouldSub=false;
		this.notify();
	}
	
	public synchronized void main(int j){
		while(bShouldSub){
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		for(int i=1;i<=100;i++){
			System.out.println("main thread sequence of "+i+",loop of"+j);
		}	
		bShouldSub=true;
		this.notify();
	}
	
	
}

